﻿using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Models.DAO
{
    public class ShowTimeDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();
        //private SettingTheaterDAO _settingTheaterDao = new SettingTheaterDAO();

        /// <summary>
        /// Get SelectList room by theaterId -> This function is used to load selectlist to combobox
        /// </summary>
        /// <param name="theaterId">TheaterId</param>
        /// <returns>List of showtime</returns>
        /// <author>vuongvt</author>
        public SelectList GetSelectListShowTimeByTheater(int theaterId)
        {
            try
            {
                List<ShowTime> listShowTime = new List<ShowTime>();
                IEnumerable<SelectListItem> selectedListShowTime = new List<SelectListItem>();
                var resultQuery = from showtime in _entities.ShowTimes
                                where showtime.TheaterId == theaterId
                                   && showtime.IsDeleted == false
                                select showtime;
                foreach (var showtime in resultQuery)
                {
                    listShowTime.Add(showtime);
                }
                selectedListShowTime = listShowTime.AsEnumerable().Select(showtime => new SelectListItem() { Value = showtime.ShowTimeId.ToString(), Text = showtime.Time.ToString() });
                return new SelectList(selectedListShowTime, "Value", "Text");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Get all show time information 
        /// </summary>
        /// <returns>List of ShowTimeConcertEntity</returns>
        /// <author>VuongVT</author>
        public List<ShowTimeConcertEntity> GetAllShowTime()
        {
            try
            {
                List<ShowTimeConcertEntity> listResult = new List<ShowTimeConcertEntity>();
                var resultQuery = (from showtime in _entities.ShowTimes
                                   join theater in _entities.Theaters on showtime.TheaterId equals theater.TheaterId
                                   where showtime.IsDeleted == false
                                        && theater.IsDeleted == false
                                   select new
                                   {
                                       ShowTimes = showtime,
                                       TheaterName = theater.TheaterName
                                   });

                foreach (var item in resultQuery)
                {
                    ShowTimeConcertEntity showtimeConcert = new ShowTimeConcertEntity();
                    showtimeConcert.ShowTimeId = item.ShowTimes.ShowTimeId;
                    showtimeConcert.Time = item.ShowTimes.Time;
                    //showtimeConcert.Date = item.ShowTimes.Date;
                    showtimeConcert.TheaterId = item.ShowTimes.TheaterId;
                    //showtimeConcert.IsActive = item.ShowTimes.IsActive;
                    showtimeConcert.IsDeleted = item.ShowTimes.IsDeleted;
                    showtimeConcert.TimeToShow = item.ShowTimes.Time.ToString(@"hh\:mm");
                    //showtimeConcert.DateToShow = item.ShowTimes.Date.Date.ToString("dd/MM/yyyy");
                    //if (item.ShowTimes.IsActive == false)
                    //{
                    //    showtimeConcert.StatusDescription = "Chưa áp dụng chiếu";
                    //}
                    //else
                    //{
                    //    showtimeConcert.StatusDescription = "Đang áp dụng chiếu";
                    //}
                    showtimeConcert.TheaterName = item.TheaterName;

                    listResult.Add(showtimeConcert);
                }
                return listResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list showtime by theaterId
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns>List of ShowTime</returns>
        /// <author>VuongVT</author> 
        public List<ShowTime> GetListShowTimeByTheaterId(int theaterId)
        {
            try
            {
                List<ShowTime> listShowTime = new List<ShowTime>();
                var resultQuery = from showtime in _entities.ShowTimes
                                  where showtime.TheaterId == theaterId
                                        && showtime.IsDeleted == false
                                  select showtime;
                foreach (var item in resultQuery)
                {
                    listShowTime.Add(item);
                }
                return listShowTime;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get list showtime by Film Id
        /// </summary>
        /// <param name="filmId"></param>
        /// <returns></returns>
        /// <author>nguyenht</author>
        /// <Edit>VuongVT</Edit>
        public List<ShowTime> GetListShowTimeByFilmId(int filmId)
        {
            try
            {
                List<ShowTime> listShowTime = new List<ShowTime>();
                var resultSearch = from schedule in _entities.Schedules
                                   from showtime in _entities.ShowTimes
                                   where schedule.FilmId == filmId
                                         && schedule.IsDeleted == false
                                         && schedule.IsActive == true
                                         && showtime.ShowTimeId == schedule.ScheduleId
                                         && showtime.IsDeleted == false
                                   select showtime;
                //var resultSearch = _entities.Schedules.Where(schedule => schedule.FilmId == filmId
                //                                                         && schedule.IsDeleted == false
                //                                                         && schedule.IsActive == true).Select(result => result.ShowTime);
                foreach (var item in resultSearch)
                {
                    listShowTime.Add(item);
                }
                return listShowTime;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get a Showtime by ShowTimeId
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns>ShowTime</returns>
        /// <author>VuongVT</author>
        public ShowTime GetShowTimeById(int showtimeId)
        {
            try
            {
                var resultQuery = (from showtime in _entities.ShowTimes
                                   where showtime.ShowTimeId == showtimeId
                                        && showtime.IsDeleted == false
                                   select showtime).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add new ShowTime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>1: success -- 0: fail</returns>
        /// <author>VuongVT</author>
        public int CreateShowTime(ShowTime showtime)
        {
            try
            {
                _entities.ShowTimes.Add(showtime);
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete a showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>1: success -- 0: fail</returns>
        /// <author>VuongVT</author>
        public int DeleteShowTime(ShowTime showtime)
        {
            try
            {
                showtime.IsDeleted = true;
                _entities.Entry(showtime).State = System.Data.EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>1: success -- 0: fail</returns>
        /// <author>VuongVT</author>
        public int EditShowTime(ShowTime showtime)
        {
            try
            {
                _entities.Entry(showtime).State = System.Data.EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check exist ShowTime when create
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>True: IsExist -- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsExistShowTimeCreate(ShowTime showtime)
        {
            try
            {
                bool resultQuery = _entities.ShowTimes.Any(s => s.Time == showtime.Time
                                                           && s.TheaterId == showtime.TheaterId
                                                           && s.IsDeleted == false);
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///// <summary>
        ///// Check Date of ShowTime is smaller than Deadline for create or not
        ///// This function is used when staff create ShowTime
        ///// </summary>
        ///// <param name="showtime"></param>
        ///// <returns>True: Smaller --- False: Bigger</returns>
        //public bool IsSmallerThanDeadline(ShowTime showtime)
        //{
        //    try
        //    {
        //        SettingTheater settingTheater = _settingTheaterDao.GetSettingTheaterById(showtime.TheaterId);
        //        System.TimeSpan duration = new System.TimeSpan(settingTheater.DeadlineCreateTicket, 0, 0, 0);
        //        if (showtime.Date < DateTime.Today.Add(duration))
        //        {
        //            return true;
        //        }
        //        return false;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        /// <summary>
        /// Check exist ShowTime when Update showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>True: IsExist -- False: Not Exist</returns>
        public bool IsExistShowTimeUpdate(ShowTime showtime)
        {
            try
            {
                var resultQuery = _entities.ShowTimes.Any(s => s.Time == showtime.Time
                                                          && s.ShowTimeId != showtime.ShowTimeId
                                                          && s.TheaterId == showtime.TheaterId
                                                          && s.IsDeleted == false);
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Check the ShowTimeId is used in Schedule  and have Booked ticket or not
        /// This function is used for user Click to Edit in List ShowTIme
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsUserInScheduleAndBooked(int showTimeId)
        {
            try
            {
                bool isUsedSchedule = false;

                var scheduleId = (from schedule in _entities.Schedules
                                  where schedule.ShowTimeId == showTimeId
                                        && schedule.IsDeleted == false
                                        && schedule.IsCreateTicket == true
                                  select schedule.ScheduleId).FirstOrDefault();
                if (scheduleId != 0)
                {
                    var isBookedTicket = _entities.Tickets.Any(ticket => ticket.ScheduleId == scheduleId
                                                                     && ticket.IsDeleted == false
                                                                     && ticket.Status == true);
                    if (isBookedTicket)
                    {
                        isUsedSchedule = true;
                    }
                }
                return isUsedSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check the ShowTimeId is used in Schedule or not
        /// This function is used for user Click to Delete in List ShowTIme
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsUsedForSchedule(int showtimeId)
        {
            try
            {
                var isUsedForSchedule = _entities.Schedules.Any(schedule => schedule.ShowTimeId == showtimeId
                                                                            && schedule.IsDeleted == false);
                return isUsedForSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///// <summary>
        ///// Check the showtime is comming soon or showing
        ///// </summary>
        ///// <param name="showTimeId"></param>
        ///// <returns>True: Showing -- False: Comming soon</returns>
        ///// <author>VuongVT</author>
        //public bool IsShowingShowTime(int showTimeId)
        //{
        //    try
        //    {
        //        var resultQuery = (from showtime in _entities.ShowTimes
        //                          where showtime.ShowTimeId == showTimeId
        //                                && showtime.IsDeleted == false
        //                          select showtime.IsActive).FirstOrDefault();
        //        return resultQuery;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
    }
}